﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace PSE.Deployment.SetupBase.Configuration.Actions
{
    [Serializable]
    public class UpdateConfig : ActionBase
    {
        public List<XmlFile> XmlFiles { get; set; }

        public override bool HasFile(string filePath)
        {
            List<XmlFile> usedXmlFiles = new List<XmlFile>();

            if (string.IsNullOrEmpty(filePath) || this.XmlFiles == null || this.XmlFiles.Count == 0)
                return false;
            else
            {
                //TODO:CODIGO MARRETADO - Quando for alterado para o filePath conter o id do sistema, remover a utilizacao do metodo RemoveRootLevel
                foreach (XmlFile xml in this.XmlFiles)
                {
                    if (xml.Folder.Files != null && xml.Folder.Files.Count > 0)
                    {
                        foreach (string file in xml.Folder.Files)
                        {
                            if (PSE.Deployment.Util.File.RemoveRootLevel(file).Equals(filePath, StringComparison.CurrentCultureIgnoreCase))
                                return true;
                        }
                    }
                    else
                    {
                        if (PSE.Deployment.Util.File.RemoveRootLevel(xml.Folder.Destination).Length > 0)
                        {
                            if (filePath.StartsWith(PSE.Deployment.Util.File.RemoveRootLevel(xml.Folder.Destination), StringComparison.CurrentCultureIgnoreCase))
                            {
                                usedXmlFiles.Add(xml);
                                this.XmlFiles = usedXmlFiles;
                                return true;
                            }
                        }
                    }
                }

                return false;

                //return this.XmlFiles.Exists(xml => ((xml.Folder.Files != null && xml.Folder.Files.Count > 0) && xml.Folder.Files.Contains(filePath, StringComparer.CurrentCultureIgnoreCase)) ||
                //                                   ((xml.Folder.Files == null || xml.Folder.Files.Count == 0) && filePath.StartsWith(xml.Folder.Destination.Remove(0, 1 + xml.Folder.Destination.IndexOf(Path.DirectorySeparatorChar)), StringComparison.CurrentCultureIgnoreCase))
                //                            );
            }
        }
    }
}
